package cn.neusoft.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 判断某个值是否属于枚举类
 */
public class EnumUtils {
    public static <T> boolean isInclude(Class<?> enumClass, T code){
        boolean include = false;
        if(enumClass.isEnum()){
            Object[] enumConstants = enumClass.getEnumConstants();
            Method getCode = null;
            try {
                getCode = enumClass.getMethod("getCode");
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            }
            for (Object enumConstant:enumConstants){
                try {
                    if (getCode.invoke(enumConstant).equals(code)) {
                        include = true;
                        break;
                    }
                } catch (IllegalAccessException | InvocationTargetException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        return include;
    }

}
